package com.united.ucts.modules.system.controller;

import com.united.frame.verify.configuration.VerifyCodeConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.Serializable;

/**
 * 自定义的登录验证码生成服务类
 *
 * @author hudan
 * @version 1.0
 * @filename VerCodeController.java
 * @copyright www.united.com
 * @date 2019/9/18
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/captcha")
public class VerCodeController implements Serializable {

    private final VerifyCodeConfiguration verifyCodeConfiguration;

    /**
     * 生成请求验证码
     *
     * @param request  当前验证请求
     * @param response 结果相应
     */
    @RequestMapping("/create")
    public void create(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        // 在内存中创建图象
        final BufferedImage image = verifyCodeConfiguration.createVerifyImg(request);

        // 输出图象到页面
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }
}